<html>
<head><meta charset="utf-8"><title>How to return a collection of graphemes · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20a.20collection.20of.20graphemes.html">How to return a collection of graphemes</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="240367894"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20a%20collection%20of%20graphemes/near/240367894" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> miles <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20a.20collection.20of.20graphemes.html#240367894">(May 26 2021 at 18:20)</a>:</h4>
<p>I'm having trouble figuring out how to fix this function's compilation error:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">use</span><span class="w"> </span><span class="n">std</span>::<span class="n">collections</span>::<span class="n">HashSet</span><span class="p">;</span><span class="w"></span>
<span class="k">use</span><span class="w"> </span><span class="n">unicode_segmentation</span>::<span class="n">UnicodeSegmentation</span><span class="p">;</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">letters_as_set</span><span class="p">(</span><span class="n">word</span>: <span class="kp">&amp;</span><span class="kt">str</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">HashSet</span><span class="o">&lt;&amp;</span><span class="kt">str</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">word</span><span class="p">.</span><span class="n">to_lowercase</span><span class="p">().</span><span class="n">graphemes</span><span class="p">(</span><span class="kc">true</span><span class="p">).</span><span class="n">collect</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>For reference, this is the original <code>char</code> version that builds successfully:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">letters_as_set</span><span class="p">(</span><span class="n">word</span>: <span class="kp">&amp;</span><span class="kt">str</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">HashSet</span><span class="o">&lt;</span><span class="kt">char</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">word</span><span class="p">.</span><span class="n">to_lowercase</span><span class="p">().</span><span class="n">chars</span><span class="p">().</span><span class="n">collect</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="240368598"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20a%20collection%20of%20graphemes/near/240368598" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20a.20collection.20of.20graphemes.html#240368598">(May 26 2021 at 18:25)</a>:</h4>
<p><a href="https://stackoverflow.com/q/32682876/155423">Is there any way to return a reference to a variable created in a function?</a></p>
<p><a href="https://stackoverflow.com/q/29428227/155423">Return local String as a slice (&amp;str)</a></p>



<a name="240368694"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20a%20collection%20of%20graphemes/near/240368694" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20a.20collection.20of.20graphemes.html#240368694">(May 26 2021 at 18:26)</a>:</h4>
<p>you create a <code>String</code> via <code>to_lowercase</code>. That string is dropped at the end of the function, invalidating the references</p>



<a name="240368792"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20a%20collection%20of%20graphemes/near/240368792" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20a.20collection.20of.20graphemes.html#240368792">(May 26 2021 at 18:26)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">letters_as_set</span><span class="p">(</span><span class="n">word</span>: <span class="kp">&amp;</span><span class="kt">str</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">HashSet</span><span class="o">&lt;</span><span class="nb">String</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">word</span><span class="p">.</span><span class="n">to_lowercase</span><span class="p">().</span><span class="n">graphemes</span><span class="p">(</span><span class="kc">true</span><span class="p">).</span><span class="n">map</span><span class="p">(</span><span class="nb">Into</span>::<span class="n">into</span><span class="p">).</span><span class="n">collect</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>The simplest change to get it to work ^</p>



<a name="240369634"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20a%20collection%20of%20graphemes/near/240369634" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> miles <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20a.20collection.20of.20graphemes.html#240369634">(May 26 2021 at 18:31)</a>:</h4>
<p>Why does the <code>chars()</code> version work then? Is that because it's taking values from the original string, rather than references (assuming <code>graphemes()</code> takes references)?</p>



<a name="240370375"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20a%20collection%20of%20graphemes/near/240370375" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20a.20collection.20of.20graphemes.html#240370375">(May 26 2021 at 18:37)</a>:</h4>
<p>the <code>chars</code> are owned values, completely separate from the string.</p>



<a name="240370470"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20a%20collection%20of%20graphemes/near/240370470" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20a.20collection.20of.20graphemes.html#240370470">(May 26 2021 at 18:37)</a>:</h4>
<p>And no need to assume when you <a href="https://docs.rs/unicode-segmentation/1.7.1/unicode_segmentation/trait.UnicodeSegmentation.html#tymethod.graphemes">have the docs</a>:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">graphemes</span><span class="o">&lt;'</span><span class="na">a</span><span class="o">&gt;</span><span class="p">(</span><span class="o">&amp;'</span><span class="na">a</span><span class="w"> </span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">is_extended</span>: <span class="kt">bool</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">Graphemes</span><span class="o">&lt;'</span><span class="na">a</span><span class="o">&gt;</span><span class="w"></span>
</code></pre></div>



<a name="240370937"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20a%20collection%20of%20graphemes/near/240370937" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> miles <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20a.20collection.20of.20graphemes.html#240370937">(May 26 2021 at 18:41)</a>:</h4>
<p>I guess I meant to say "assuming <code>graphemes()</code> <em>produces</em> references" from the original string. Which wasn't clear to me in the docs.<br>
Also, found that this version works too - possibly less generic than using <code>Into::into</code>:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="n">word</span><span class="p">.</span><span class="n">to_lowercase</span><span class="p">().</span><span class="n">graphemes</span><span class="p">(</span><span class="kc">true</span><span class="p">).</span><span class="n">map</span><span class="p">(</span><span class="nb">String</span>::<span class="n">from</span><span class="p">).</span><span class="n">collect</span><span class="p">()</span><span class="w"></span>
</code></pre></div>



<a name="240371157"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20a%20collection%20of%20graphemes/near/240371157" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20a.20collection.20of.20graphemes.html#240371157">(May 26 2021 at 18:42)</a>:</h4>
<p>depending on what you mean by "generic". Those do the same thing.</p>



<a name="240371264"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20a%20collection%20of%20graphemes/near/240371264" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20a.20collection.20of.20graphemes.html#240371264">(May 26 2021 at 18:43)</a>:</h4>
<blockquote>
<p>Which wasn't clear to me in the docs</p>
</blockquote>
<p>It's worth understanding that. The <code>'a</code> shows how lifetimes flow into and then out of the function.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>